// import com.alibaba.druid.DbType;
// import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
// import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
// import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerASTVisitor;
// import com.alibaba.druid.sql.visitor.SQLASTVisitor;

#include "SQLServerSelectQueryBlock.h"
#include "../../../../utils/instanceof.h"
#include "../../../../utils/listTran.h"
#include "SQLServerTop.h"
#include "../../../../Exception/UnsupportedOperationException.h"
#include "../visitor/SQLServerASTVisitor.h"
#include "../../../ast/statement/SQLSelectItem.h"

SQLServerSelectQueryBlock::SQLServerSelectQueryBlock()
{
  dbType = DbType_ptr(new DbType(&DbType::sqlserver));
}

SQLServerTop_ptr SQLServerSelectQueryBlock::getTop()
{
  return top;
}

void SQLServerSelectQueryBlock::setTop(SQLServerTop_ptr top)
{
  if (top != nullptr)
  {
    top->setParent(SharedObject(SQLServerSelectQueryBlock));
  }
  this->top = top;
}

void SQLServerSelectQueryBlock::setTop(int rowCount)
{
  setTop(SQLServerTop_ptr(new SQLServerTop(SQLIntegerExpr_ptr(new SQLIntegerExpr(rowCount)))));
}

// @Override
void SQLServerSelectQueryBlock::accept0(SQLASTVisitor_ptr visitor)
{
  if (instanceof <SQLASTVisitor, SQLServerASTVisitor>(visitor))
  {
    accept0(std::dynamic_pointer_cast<SQLServerASTVisitor>(visitor));
  }
  else
  {
    SQLSelectQueryBlock::accept0(visitor);
  }
}

void SQLServerSelectQueryBlock::accept0(SQLServerASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SQLServerSelectQueryBlock)))
  {
    acceptChild(visitor, this->top);
    SQLObject_list_ptr tmp1 = ListTran::tran_class<SQLSelectItem, SQLObject>(this->selectList);
    acceptChild(visitor, tmp1);
    acceptChild(visitor, this->from);
    acceptChild(visitor, this->where);
    acceptChild(visitor, this->groupBy);
  }
  visitor->endVisit(SharedObject(SQLServerSelectQueryBlock));
}

void SQLServerSelectQueryBlock::limit(int rowCount, int offset)
{
  if (offset <= 0)
  {
    setTop(rowCount);
  }
  else
  {
    throw new UnsupportedOperationException(make_string_ptr("not support offset"));
  }
}
